package com.vstudy.zx.controller;

import com.vstudy.zx.entity.Article;
import com.vstudy.zx.entity.Grade;
import com.vstudy.zx.entity.Outline;
import com.vstudy.zx.entity.Subject;
import com.vstudy.zx.service.CatalogService;
import com.vstudy.zx.utils.page.ArticlePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "/catalog")
public class CatalogController {
    @Autowired
    private CatalogService service;

    // 不提供数据，提供条件
    @RequestMapping(value = "/{gradeId}/{subjectId}", method = RequestMethod.GET)
    public String index(@PathVariable("subjectId") Long sId, @PathVariable("gradeId") Integer gId, Model model) {
        Subject subject = service.getSubjectById(sId);
        Grade grade = service.getGradeById(gId);
        model.addAttribute("subject", subject);
        model.addAttribute("grade", grade);

        return "zx/catalog/index.html";
    }

    @RequestMapping(value = "/outline", method = RequestMethod.GET)
    public String outline(Long oid, Model model) {
        model.addAttribute("oid", oid);
        return "zx/catalog/outline.jsp";
    }

    // subjectId, gradeId, outlineId
    @RequestMapping(value = "/articles", method = RequestMethod.POST)
    @ResponseBody
    public Object getArticles(Long subjectId, Integer gradeId, Long outlineId, ArticlePage page, HttpServletRequest request) {
        List<Article> articles = service.getArticlesPage(page, gradeId, subjectId, request);
        return articles;
    }

    @RequestMapping(value = "/articlesByOid", method = RequestMethod.POST)
    @ResponseBody
    public Object getArticlesByOid(Long oid, ArticlePage page, HttpServletRequest request) {
        List<Article> articles = service.getArticlesPage(page, oid, request);
        return articles;
    }

    // 大纲相关
    @RequestMapping(value = "/articles", method = RequestMethod.GET)
    public String articles(Long outlineId, Model model, HttpServletRequest request) {
        List<Article> articles = service.getArticlesByOutlineId(outlineId, request);
        Outline outline = service.getOutlineById(outlineId);
        model.addAttribute("articles", articles);
        model.addAttribute("outline", outline);
        return "zx/catalog/outlineArticles.jsp";
    }


    @RequestMapping(value = "/outlinesBySubjectId", method = RequestMethod.POST)
    @ResponseBody
    public List<Outline> getOutliesBySubjectId(Long subjectId) {
        List<Outline> outlines = service.getOutlinesBySubjectId(subjectId);
        return outlines;
    }

}
